package player.view.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import player.model.Playlist;
import player.util.PlaylistLoader;
import player.view.MainWindow;

public class OpenPlaylistAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4943043951075819258L;

	private MainWindow owner;
	
	public OpenPlaylistAction(MainWindow owner){
		this.setOwner(owner);
		
		putValue(NAME, "Open Playlist");
		putValue(SHORT_DESCRIPTION, "Otvaranje plejliste");
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setAcceptAllFileFilterUsed(false);
		fc.setApproveButtonText("Otvori");
		fc.setDialogTitle("Otvaranje plejliste");
		fc.setFileFilter(new FileNameExtensionFilter("Playlist fajlovi", "pl"));
		int returnVal = fc.showOpenDialog(owner);
		
		if(returnVal == JFileChooser.APPROVE_OPTION){
			try {
				Playlist pl = PlaylistLoader.loadPlaylist(fc.getSelectedFile().getAbsolutePath());
				owner.getPlayer().setPlaylist(pl);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
		}
	}

	public void setOwner(MainWindow owner) {
		this.owner = owner;
	}

	public MainWindow getOwner() {
		return owner;
	}

}
